package ;

import events.ResizeEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.Lib;
import graphic.RenderObject;
import graphic.RessourceManager;
import graphic.Scene;

/**
 * @author Damien Mabin
 */

class Main 
{
	static var m_RessourceManager 	: RessourceManager;
	static var m_Scene				: Scene;
	
	static function main() 
	{
		m_RessourceManager = RessourceManager.GetInstance();
		m_RessourceManager.addEventListener(RessourceManagerEvent.INITIALISATION_COMPLETE, OnInitialisationComplete);
	}
	
	static function OnInitialisationComplete(_Evt:Event):Void
	{
		m_Scene = new Scene("ressources/gameDesign/Scene1.xml");
		m_Scene.addEventListener(SceneEvent.INITIALISATION_COMPLETE, OnSceneInitialisationComplete);
	}
	
	static private function OnSceneInitialisationComplete(_Evt:Event):Void
	{
		m_RessourceManager.addEventListener(RessourceManagerEvent.PREPARATION_COMPLETE, OnRessourceManagerReadyToInstaciateScene);
		m_RessourceManager.PrepareRenderObjects(m_Scene.GetNeededRenderObject());
	}
	
	static function OnRessourceManagerReadyToInstaciateScene(_Evt:Event):Void
	{
		Lib.current.stage.addChild(m_Scene);
		m_Scene.addEventListener(MouseEvent.CLICK, OnMouseEvent);
		m_Scene.Instanciate();
		m_Scene.dispatchEvent(new ResizeEvent(1024, 768));
		Lib.current.stage.addEventListener(Event.ENTER_FRAME, OnEnterFrameEvent);
	}
	
	static function OnEnterFrameEvent(_Evt:Event):Void
	{
		m_Scene.Update(Std.int(1000 / Lib.current.stage.frameRate));
	}
	
	static function OnMouseEvent(_Evt:MouseEvent):Void
	{
		for (ro in m_Scene.m_CurrentRenderObjects)
		{
			ro.PlayAnimation("Jump", 1);
		}
	}
	
}